package gui.mainWindow.targetAssignment;

import java.awt.Component;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

import tournamentData.Target;
import tournamentData.TournamentData;
import core.CustomIcons;

public class TargetNumberDropDownRenderer extends JLabel implements ListCellRenderer {

	public TargetNumberDropDownRenderer() {
		this.setOpaque(true);
		this.setVerticalAlignment(CENTER);
	}

	/*
	 * This method finds the image and text corresponding to the selected value
	 * and returns the label, set up to display the text and image.
	 */
	public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
		// Get the selected index. (The index param isn't
		// always valid, so just use the value.)
		Integer selectedIndex;
		try {
			selectedIndex = Integer.parseInt(value.toString());
		} catch (Exception e) {
			selectedIndex = 0;
		}

		if (isSelected) {
			this.setBackground(list.getSelectionBackground());
			this.setForeground(list.getSelectionForeground());
		} else {
			this.setBackground(list.getBackground());
			this.setForeground(list.getForeground());
		}

		int playersPerTarget = TournamentData.getInstance().getPlayersPerTarget();
		Integer assigned = 0;
		for (int i = 0; i < playersPerTarget; i++) {
			if (TournamentData.getInstance().getTargetAssignment().hasPlayerAssigned(new Target(Integer.parseInt(value.toString()), (char) ('A' + i)))) {
				assigned++;
			}
		}

		if (assigned == 0) {
			this.setIcon(CustomIcons.greenArrow);
		}
		if (assigned == playersPerTarget) {
			this.setIcon(CustomIcons.redArrow);
		}
		if ((assigned > 0) && (assigned < playersPerTarget)) {
			this.setIcon(CustomIcons.yellowArrow);
		}
		this.setText(selectedIndex.toString());
		this.setFont(list.getFont());

		return this;
	}

}